﻿import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  ComboboxItemDto, LibraryOfLaborServiceProxy, ProductionOutput, RouteOfLibraryServiceProxy, RouteEdit, SaveRoutesInput, EditRoutesStringDto} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { from } from 'rxjs';

@Component({
    selector: 'editRouteModal',
    templateUrl: './edit-route-modal.component.html'
})
export class EditRouteModalComponent extends AppComponentBase {

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @ViewChild('editRouteModal') modal: ModalDirective;
    
    active = false;
    saving = false;

    isTemporaryLaborHour = true;

    routes:RouteEdit[] = new Array<RouteEdit>();
    productionId :string = "";
    routeString :string = "";

    professions = [
        {label: '飞机钣金', value: '飞机钣金'},
        {label: '机械加工', value: '机械加工'},
        {label: '表面处理', value: '表面处理'},
        {label: '热处理', value: '热处理'},
        {label: '焊接', value: '焊接'},
        {label: '装配', value: '装配'}
    ];
  
    constructor(
        injector: Injector,
        private _routeOfLibraryServiceProxy: RouteOfLibraryServiceProxy
    ) {
        super(injector);
    }

    showEditRoute(productionId: string,routeString: string,isTemporaryLaborHour:boolean):void{
        this.productionId = productionId;
        this.routeString = routeString;
        this.isTemporaryLaborHour = isTemporaryLaborHour;

        this.active = true;
        this.modal.show();
        this._routeOfLibraryServiceProxy.getAllRoutesByProduction(productionId).subscribe(result=>
                {
                    this.routes = result;
                }
            );          
    }

    onDeleteRoute(rowData:RouteEdit):void
    {
        this.routes = this.routes.filter(c=>c != rowData);        
    }

    generateRoutes(): void {
        let routeList = this.routeString.split('-');

        if (this.routes == null) {
            this.routes = new Array<RouteEdit>();

        }

        routeList.forEach(element => {
            if (this.routes.filter(c => c.dpt == element).length == 0) {
                let newRoute = new RouteEdit();
                newRoute.dpt = element;
                newRoute.productionId = this.productionId;
                this.routes.push(newRoute);
            }
        });

    }

    
   saveRouteString():void{
       let newRouteString = new EditRoutesStringDto();
       newRouteString.productionId = this.productionId;
       newRouteString.routeString = this.routeString;
       this._routeOfLibraryServiceProxy.updateRouteString(newRouteString).subscribe(result=>
            {
                this.notify.info(this.l('SavedSuccessfully'));
                this.modalSave.emit(null);
            }
        );
   }

    saveRoute():void{
        this.saving = true;
        let saveRoutes = new SaveRoutesInput();
        saveRoutes.productionId =  this.productionId;
        saveRoutes.routeEdits = this.routes;
        this._routeOfLibraryServiceProxy.saveRoutes(saveRoutes).subscribe(result=>
            {
                this.saving = false;
                this._routeOfLibraryServiceProxy.getAllRoutesByProduction(this.productionId).subscribe(result=>
                    {
                        this.routes = result;
                        this.notify.info(this.l('SavedSuccessfully'));
                        this.modalSave.emit(null);
                    }
                );    
            }
        );           
    }

    changeIstemp():void{
        this._routeOfLibraryServiceProxy.changeTemporaryLaborHour(this.productionId).subscribe(result=>
            {
                this.notify.info(this.l('SavedSuccessfully'));
            }
        );  
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
